package org.jeecg.modules.courseplan.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.jeecg.modules.common.utils.DateUtil;
import org.jeecg.modules.courseplan.entity.CouCoursePlan;
import org.jeecg.modules.courseplan.mapper.CouCoursePlanMapper;
import org.jeecg.modules.courseplan.service.ICouCoursePlanService;
import org.jeecg.modules.courseplan.vo.CouCoursePlanVo;
import org.jeecg.modules.courseplanatom.entity.CouCoursePlanAtom;
import org.jeecg.modules.courseplanatom.service.ICouCoursePlanAtomService;
import org.jeecg.modules.relation.relbuildingclassroom.vo.RelBuildingClassroomVo;
import org.jeecg.modules.relation.relcourseplanapp.entity.RelCoursePlanApp;
import org.jeecg.modules.relation.relcourseplanapp.service.IRelCoursePlanAppService;
import org.jeecg.modules.schoolyear.entity.CouSchoolYear;
import org.jeecg.modules.schoolyear.service.ICouSchoolYearService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.util.Assert;

import java.util.ArrayList;
import java.util.List;

/**
 * @Description: cou_course_plan
 * @author: baochangrong
 * @Date: 2022-02-17
 * @Version: V1.0
 */
@Service
public class CouCoursePlanServiceImpl extends ServiceImpl<CouCoursePlanMapper, CouCoursePlan> implements ICouCoursePlanService {
    @Autowired
    CouCoursePlanMapper couCoursePlanMapper;
    @Autowired
    private ICouSchoolYearService iCouSchoolYearServiceImpl;

    @Autowired
    private ICouCoursePlanAtomService iCouCoursePlanAtomServiceImpl;
    @Autowired
    private IRelCoursePlanAppService iRelCoursePlanAppServiceImpl;

    /**
     * 分页 查询全部
     */
    @Override
    public IPage<CouCoursePlanVo> listAll(Page<CouCoursePlanVo> page, String tenantId) {
        //获取该租户下所有建筑id
        List<CouCoursePlanVo> couCoursePlanVoList = couCoursePlanMapper.listAll(tenantId);
        Integer total = couCoursePlanVoList.size();
        IPage<CouCoursePlanVo> result = new Page<>(page.getCurrent(), page.getSize(), total);
        result.setRecords(couCoursePlanVoList);
        return result;
    }

    /**
     * 教学计划相关 queryList
     */
    @Override
    public List<CouCoursePlan> queryList(String tenantId) {
        List<CouCoursePlan> couCoursePlanList = couCoursePlanMapper.queryList(tenantId);
        return couCoursePlanList;
    }

    /**
     * 逻辑删除
     */
    @Override
    public boolean planIzdelete(CouCoursePlan couCoursePlan) {
        boolean _delete = couCoursePlanMapper.planIzdelete(couCoursePlan);
        return _delete;
    }

    @Override
    public int insertUpgradeCouCoursePlan(CouCoursePlanVo couCoursePlanVo) {
        QueryWrapper<CouSchoolYear> couSchoolYearWrapper = new QueryWrapper();
        couSchoolYearWrapper.eq("id", couCoursePlanVo.getSchoolYearId());
        couSchoolYearWrapper.eq("tenant_id", couCoursePlanVo.getTenantId());
        List<CouSchoolYear> schoolYearList = iCouSchoolYearServiceImpl.list(couSchoolYearWrapper);
        if (schoolYearList == null || schoolYearList.size() < 1) {
            new IllegalArgumentException("未查询到学年对应的相关信息，请重新选择学年！");
            return 0;
        }

        QueryWrapper<CouSchoolYear> couSchoolYearWrapperOld = new QueryWrapper();
        couSchoolYearWrapperOld.eq("school_year_code", Integer.parseInt(schoolYearList.get(0).getSchoolYearCode()) - 1);
        couSchoolYearWrapperOld.eq("tenant_id", couCoursePlanVo.getTenantId());
        List<CouSchoolYear> schoolYearListOld = iCouSchoolYearServiceImpl.list(couSchoolYearWrapperOld);
        if (schoolYearListOld == null || schoolYearListOld.size() < 1) {
            new IllegalArgumentException("未查询到升级学年对应的上一学年信息,无法完成数据复制，请确认！");
            return 0;
        }
        QueryWrapper<CouCoursePlan> couCoursePlanQueryWrapper = new QueryWrapper<>();
        couCoursePlanQueryWrapper.eq("semester_id", couCoursePlanVo.getSemesterId());
        couCoursePlanQueryWrapper.eq("school_year_id", schoolYearListOld.get(0).getId());
        couCoursePlanQueryWrapper.eq("grade_id", couCoursePlanVo.getGradeId());
        List<CouCoursePlan> couCoursePlanList = couCoursePlanMapper.selectList(couCoursePlanQueryWrapper);
        if (couCoursePlanList == null || couCoursePlanList.size() < 1) {
            new IllegalArgumentException("未查询到升级学年对应的上一学年教学计划信息,无法完成数据复制，请确认！");
            return 0;
        }
        for (CouCoursePlan coursePlan : couCoursePlanList) {
            //雪花算法拿到新的教学计划ID
            String newPlanId = IdWorker.getIdStr();
            String oldPlanId = coursePlan.getId();
            coursePlan.setId(newPlanId);
            coursePlan.setCoursePlanCode(DateUtil.getNow("YYYYMM"));
            coursePlan.setCreateTime(DateUtil.getCurrentDate());
            if (couCoursePlanMapper.insert(coursePlan) < 1) {
                new IllegalArgumentException("复制新数据失败，请确认！" + oldPlanId + "对应的数据信息");
                return 0;
            }
            CouCoursePlanAtom couCoursePlanAtom = new CouCoursePlanAtom();
            couCoursePlanAtom.setOldCoursePlanId(oldPlanId);
            couCoursePlanAtom.setNewCoursePlanId(newPlanId);
            if (iCouCoursePlanAtomServiceImpl.insertUpgradeCouCoursePlanAtom(couCoursePlanAtom) < 1) {
                new IllegalArgumentException("复制新数据到教学计划详情失败，请确认！" + oldPlanId + "对应的数据信息");
                return 0;
            }
            RelCoursePlanApp relCoursePlanApp = new RelCoursePlanApp();
            relCoursePlanApp.setOldCoursePlanId(oldPlanId);
            relCoursePlanApp.setNewCoursePlanId(newPlanId);
            if (iRelCoursePlanAppServiceImpl.insertUpgradeRelCoursePlanApp(relCoursePlanApp) < 1) {
                new IllegalArgumentException("复制新数据到教学计划下达失败，请确认！" + oldPlanId + "对应的数据信息");
                return 0;
            }
        }

        return 1;
    }


}
